import { axiosResourceIns } from '../axios';

const URL: Record<keyof IService.Resource.HTTP, string> = {
    getDeviceLibraryList: '/device/devlib-file-manager/page',
    createDeviceLibrary: '/device/devlib-file-manager/add',
    deleteDeviceLibrary: '/device/devlib-file-manager/delete',
    getDeviceLibraryFileUploadUrl: '/device/devlib-file-manager/file/getHWYunUploadAuthUrlAndFileId',
    getDeviceLibraryFileDownloadUrl: '/device/devlib-file-manager/file/getFileDownloadAuthUrlHUAWEI',
    updateDeviceLibraryFile: '/device/devlib-file-manager/file/upload',
    getSceneLibraryList: '/scenes/sceneslib-file-manager/page',
    createSceneLibrary: '/scenes/sceneslib-file-manager/add',
    deleteSceneLibrary: '/scenes/sceneslib-file-manager/delete',
    getSceneLibraryFileUploadUrl: '/scenes/sceneslib-file-manager/file/getHWYunUploadAuthUrlAndFileId',
    getSceneLibraryFileDownloadUrl: '/scenes/sceneslib-file-manager/file/getFileDownloadAuthUrlHUAWEI',
    updateSceneLibraryFile: '/scenes/sceneslib-file-manager/file/upload',
};

export async function getDeviceLibraryList(request: IService.Resource.HTTP['getDeviceLibraryList']['request']): Promise<IService.Resource.HTTP['getDeviceLibraryList']['response']> {
    return axiosResourceIns.post(URL.getDeviceLibraryList, { searchKey: request.searchKey }, { params: { current: request.current, size: request.size } });
}

export async function createDeviceLibrary(request: IService.Resource.HTTP['createDeviceLibrary']['request']): Promise<IService.Resource.HTTP['getDeviceLibraryList']['response']> {
    return axiosResourceIns.post(URL.createDeviceLibrary, request);
}

export async function deleteDeviceLibrary(request: IService.Resource.HTTP['deleteDeviceLibrary']['request']): Promise<IService.Resource.HTTP['deleteDeviceLibrary']['response']> {
    return axiosResourceIns.post(URL.deleteDeviceLibrary, request);
}

export async function getDeviceLibraryFileUploadUrl(
    request: IService.Resource.HTTP['getDeviceLibraryFileUploadUrl']['request']
): Promise<IService.Resource.HTTP['getDeviceLibraryFileUploadUrl']['response']> {
    return axiosResourceIns.post(URL.getDeviceLibraryFileUploadUrl, request);
}

export async function getDeviceLibraryFileDownloadUrl(
    request: IService.Resource.HTTP['getDeviceLibraryFileDownloadUrl']['request']
): Promise<IService.Resource.HTTP['getDeviceLibraryFileDownloadUrl']['response']> {
    return axiosResourceIns.post(URL.getDeviceLibraryFileDownloadUrl, request);
}

export async function updateDeviceLibraryFile(request: IService.Resource.HTTP['updateDeviceLibraryFile']['request']): Promise<IService.Resource.HTTP['updateDeviceLibraryFile']['response']> {
    const formData = new FormData();
    formData.append('fileId', request.fileId);
    formData.append('file', request.file);
    return axiosResourceIns.post(URL.updateDeviceLibraryFile, request, { headers: { 'Content-Type': 'multipart/form-data' } });
}

export async function getSceneLibraryList(request: IService.Resource.HTTP['getSceneLibraryList']['request']): Promise<IService.Resource.HTTP['getSceneLibraryList']['response']> {
    return axiosResourceIns.post(URL.getSceneLibraryList, { searchKey: request.searchKey }, { params: { current: request.current, size: request.size } });
}

export async function createSceneLibrary(request: IService.Resource.HTTP['createSceneLibrary']['request']): Promise<IService.Resource.HTTP['createSceneLibrary']['response']> {
    return axiosResourceIns.post(URL.createSceneLibrary, request);
}

export async function deleteSceneLibrary(request: IService.Resource.HTTP['deleteSceneLibrary']['request']): Promise<IService.Resource.HTTP['deleteSceneLibrary']['response']> {
    return axiosResourceIns.post(URL.deleteSceneLibrary, request);
}

export async function getSceneLibraryFileUploadUrl(
    request: IService.Resource.HTTP['getSceneLibraryFileUploadUrl']['request']
): Promise<IService.Resource.HTTP['getSceneLibraryFileUploadUrl']['response']> {
    return axiosResourceIns.post(URL.getSceneLibraryFileUploadUrl, request);
}

export async function getSceneLibraryFileDownloadUrl(
    request: IService.Resource.HTTP['getSceneLibraryFileDownloadUrl']['request']
): Promise<IService.Resource.HTTP['getSceneLibraryFileDownloadUrl']['response']> {
    return axiosResourceIns.post(URL.getSceneLibraryFileDownloadUrl, request);
}

export async function updateSceneLibraryFile(request: IService.Resource.HTTP['updateSceneLibraryFile']['request']): Promise<IService.Resource.HTTP['updateSceneLibraryFile']['response']> {
    const formData = new FormData();
    formData.append('fileId', request.fileId);
    formData.append('file', request.file);
    return axiosResourceIns.post(URL.updateDeviceLibraryFile, request, { headers: { 'Content-Type': 'multipart/form-data' } });
}
